/****************************************************************************************
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import java.util.Objects;

import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateFormatUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.FeeFailureModeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;

/**
 * <Description> <br>
 *
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年2月25日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(FeeFailureModeAttrHandler.class)
public class FeeFailureModeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    OperateInstAttrService operateInstAttrService;

    @Autowired
    IQuerySceneInstService querySceneInstService;
    /**
     * Description: <br>
     *
     * @author tengg<br>
     * @taskId <br>
     * @param attr
     * @param inst
     * @param data <br>
     */
    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String value = attr.getValue();
        this.setIsEdit(inst, data, value);
        // 起租页面资费刷新处理
        this.dealStartRentFlowNode(inst, data, value);
        // 对于存量有效资费：如果本次失效模式受理做了修改，资费项失效模式变为“起租时间-1失效”，则需要刷新资费项的失效时间=起租时间-1S（如果存量资费本次操作失效模式未做修改，则不需要刷新资费失效时间）
        this.refreshExpTime(attr, inst, data);
    }

    /**
     * 刷新失效时间
     * @param attr
     * @param inst
     * @param data
     */
    private void refreshExpTime(KiteAttr attr, KiteInst inst, AttrData data) {
        String sceneInstId = data.getSceneInstId();
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode", "");
        if (!"OrderStartRent".equals(nodeCode) || !ActionType.M.name().equals(inst.getActionType().name())) {
            return;
        }
        if (KiteStringUtils.isNotEmpty(attr.getPreValue()) && !KiteStringUtils.equals(attr.getOriginValue(), attr.getValue()) &&
                KiteStringUtils.equals(attr.getValue(), LocalKeyConsts.STARTING_TIME_INVALIDATION)) {
            KiteAttr startRentTimeAttr = querySceneInstService.getCommonAttr(sceneInstId, "REC_10057");
            StringBuffer startRentTime = new StringBuffer(startRentTimeAttr.getValue());
            if (startRentTime.length() == 10) {
                startRentTime.append(" 00:00:00");
            }
            String expireTime = DateFormatUtils.addDate(startRentTime.toString(), DateFormatUtils.DATE_TIME_FORMAT, -1, DateFormatUtils.SECOND);
            operateInstAttrService.setAttrValue(inst, LocalKeyConsts.END_DATE_FIELD_NAME, expireTime, data.getSceneInstId(), null);
        }
    }

    private void setIsEdit(KiteInst inst, AttrData data, String value) {
        if (KiteStringUtils.isEqual(inst.getActionType().name(), ActionType.A.name())) {
            return;
        }
        //生效时间
        KiteAttr starteDateKiteAttr = inst.getAttr(LocalKeyConsts.STARTING_DATE_FIELD_NAME);
        //失效时间
        KiteAttr endDateKiteAttr = inst.getAttr(LocalKeyConsts.END_DATE_FIELD_NAME);
        if (KiteStringUtils.isEqual(value, LocalKeyConsts.STARTING_TIME_INVALIDATION)) {

            if (Objects.nonNull(starteDateKiteAttr)) {
                starteDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
          //失效时间、起租时间失效间，为当前操作日期的0点-23：59：59 ,生效时间、失效时间不可修改，置灰
            if (Objects.nonNull(endDateKiteAttr)) {
                operateInstAttrService.setAttrValue(inst, LocalKeyConsts.END_DATE_FIELD_NAME, DateFormatUtils.formatDate(KiteDateUtils.getEndOfLastDay(), DateFormatUtils.DATE_TIME_FORMAT), data.getSceneInstId(), null);
                endDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
        }
        else if (KiteStringUtils.isEqual(value, LocalKeyConsts.CUSTOM_INVALIDATION)) {

            if (Objects.nonNull(starteDateKiteAttr)) {
                starteDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
            if (Objects.nonNull(endDateKiteAttr)) {
                endDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
            }
        }else if (KiteStringUtils.isEqual(value, LocalKeyConsts.IMMEDIATELY_INVALIDATION)) {
            //立即失效
            //生效时间当日0点生效，不可编辑
            if (Objects.nonNull(starteDateKiteAttr)) {
                starteDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }

            //失效时间不可编辑
            if (Objects.nonNull(endDateKiteAttr)) {
                operateInstAttrService.setAttrValue(inst, LocalKeyConsts.END_DATE_FIELD_NAME, DateFormatUtils.formatDate(KiteDateUtils.getStartOfDay(), DateFormatUtils.DATE_TIME_FORMAT), data.getSceneInstId(), null);
                endDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
        }else if (KiteStringUtils.isEqual(value, LocalKeyConsts.NEXT_MONTH_INVALIDATION)) {
            //下月生效
            //生效时间次月0点生效，不可编辑
            if (Objects.nonNull(starteDateKiteAttr)) {
                starteDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
            //失效时间不可编辑
            if (Objects.nonNull(endDateKiteAttr)) {
                operateInstAttrService.setAttrValue(inst, LocalKeyConsts.END_DATE_FIELD_NAME, DateFormatUtils.getFirstDayInNextMonth(DateFormatUtils.getFormatedDateTime(), DateFormatUtils.DATE_TIME_FORMAT), data.getSceneInstId(), null);
                endDateKiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
        }
    }

    private void dealStartRentFlowNode(KiteInst inst, AttrData data, String value) {
        String sceneInstId = data.getSceneInstId();
        String nodeCode = querySceneInstService.getExtParameterValue(sceneInstId, "nodeCode", "");
        if (!"OrderStartRent".equals(nodeCode) || ActionType.A.name().equals(inst.getActionType().name())) {
            return;
        }
        //如果是起租时间-1失效,刷新资费的失效时间
        if ((LocalKeyConsts.STARTING_TIME_INVALIDATION).equals(value)) {
            //起租时间
            KiteAttr startRentTimeAttr = querySceneInstService.getCommonAttr(sceneInstId, "REC_10057");
            //起租时间-1
            String expireTime = DateFormatUtils.addDate(startRentTimeAttr.getValue(),DateFormatUtils.DATE_FORMAT, -1, DateFormatUtils.DAY);
            operateInstAttrService.setAttrValue(inst, LocalKeyConsts.END_DATE_FIELD_NAME, expireTime, data.getSceneInstId(), null);
        }
    }

    /**
     * Description: <br>
     *
     * @author tengg<br>
     * @taskId <br>
     * @param attr
     * @param inst
     * @param data <br>
     */
    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {
        // TODO Auto-generated method stub

    }

}
